﻿using MediatR;
using Traceability.App.API.Application.Command.Enterprise;
using Traceability.App.API.DTOs;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.Enterprise
{
    public class GetYZEnterpriseHandler : IRequestHandler<GetYZEnterpriseCommand, APIResult<YZEnterpriseDTO>>
    {
        private readonly IBaseRepository<EnterpriseModel> enterpriseRepo;

        public GetYZEnterpriseHandler(IBaseRepository<EnterpriseModel> enterpriseRepo)
        {
            this.enterpriseRepo = enterpriseRepo;
        }
        public Task<APIResult<YZEnterpriseDTO>> Handle(GetYZEnterpriseCommand request, CancellationToken cancellationToken)
        {
            APIResult<YZEnterpriseDTO> result = new APIResult<YZEnterpriseDTO>();
            var res = enterpriseRepo.GetAll().FirstOrDefault(x => x.Batch == request.Batch && x.EnterpriseId == request.EnterpriseId);
            YZEnterpriseDTO yZ = new YZEnterpriseDTO();
            if (res == null)
            {

            }
            else
            {
                yZ.Id = res.Id;
                yZ.Batch = res.Batch;
                yZ.EnterpriseId = res.EnterpriseId;
                yZ.Director = res.Director;
                yZ.Farm = res.Farm;
                yZ.IHNumber = res.IHNumber;

            }

            result.Message = "显示成功";
            result.Data = yZ;
            result.Code = APIEnum.Ok;
            return Task.FromResult(result);
        }
    }
}
